package com.phenix.wanandroid.ui.main

import androidx.compose.foundation.background
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Menu
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.currentBackStackEntryAsState
import com.google.accompanist.insets.ProvideWindowInsets
import com.google.accompanist.insets.navigationBarsPadding
import com.google.accompanist.insets.statusBarsPadding
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.phenix.wanandroid.ui.state.rememberAppScaffoldState
import com.phenix.wanandroid.ui.theme.WanAndroidApiDemoTheme
import kotlinx.coroutines.launch


@Composable
fun WanAndroidApp() {
    val myAppState = rememberAppScaffoldState()
    val snackbarHostState = remember { SnackbarHostState() }
    val scope = rememberCoroutineScope()

    WanAndroidApiDemoTheme {
        ProvideWindowInsets {
            val systemUiController = rememberSystemUiController()
            val darkIcons = MaterialTheme.colors.isLight
            val systemBarsColor = MaterialTheme.colors.primary

            SideEffect {
                systemUiController.setSystemBarsColor(systemBarsColor, darkIcons = darkIcons)
            }

            Scaffold(
                scaffoldState = myAppState.scaffoldState,
                snackbarHost = { SnackbarHost(snackbarHostState) },
                modifier = Modifier
                    .statusBarsPadding()
                    .navigationBarsPadding(),
                topBar = {
                    TopAppBar(
                        title = { Text("Simple Scaffold Screen") },
                        navigationIcon = {
                            IconButton(
                                onClick = { /* "Open nav drawer" */ }
                            ) {
                                Icon(
                                    Icons.Filled.Menu,
                                    contentDescription = "Localized description"
                                )
                            }
                        }
                    )
                },
                floatingActionButtonPosition = FabPosition.End,
                floatingActionButton = {
                    var clickCount by remember { mutableStateOf(0) }
                    ExtendedFloatingActionButton(
                        text = { Text("Show snackbar") },
                        onClick = {// show snackbar as a suspend function
                            scope.launch {
                                snackbarHostState.showSnackbar(
                                    "Snackbar # ${++clickCount}"
                                )
                            }
                        }
                    )
                },
                bottomBar = {
                    if (myAppState.shouldShowBottomBar) {
                        BottomNavigation {
                            val navBackStackEntry by myAppState.navController.currentBackStackEntryAsState()
                            val currentDestination = navBackStackEntry?.destination
                            myAppState.bottomBarTabs.forEach { screen ->
                                BottomNavigationItem(
                                    selected = false, onClick = { /*TODO*/ },
                                    icon = {
                                        Icon(
                                            Icons.Filled.Favorite,
                                            contentDescription = null
                                        )
                                    },
                                    label = { Text("test") },
//                            selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
//                            onClick = {
//                                myAppState.navController.navigate(screen.route) {
//                                    // Pop up to the start destination of the graph to
//                                    // avoid building up a large stack of destinations
//                                    // on the back stack as users select items
//                                    popUpTo(myAppState.navController.graph.findStartDestination().id) {
//                                        saveState = true
//                                    }
//                                    // Avoid multiple copies of the same destination when
//                                    // reselecting the same item
//                                    launchSingleTop = true
//                                    // Restore state when reselecting a previously selected item
//                                    restoreState = true
//                                }
//                            }
                                )
                            }
                        }
                    }
                }
            ) {
                MainScreen()
//        NavHost(navController = myAppState.navController, startDestination = "initial") { /* ... */ }
            }

        }

    }
}